package com.lvmama.rhino.web;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.base.BaseControllerSupport;
import com.lvmama.rhino.entity.PVUVConditionVO;
import com.lvmama.rhino.enums.CommonStringType;
import com.lvmama.rhino.pojo.PVUV;
import com.lvmama.rhino.service.PVUVService;

@Controller
@RequestMapping("travel")
public class TravelController extends BaseControllerSupport{
    
    @Autowired
    private PVUVService pVUVService;
    
    @RequiresPermissions("sys:pvuv:view")
    @RequestMapping("pvUvList")
    public String pvUvList(ModelMap model ,PVUVConditionVO pVUVConditionVO){
        CommonStringType[] getFirstChannelType = CommonStringType.getFirstChannelType();
        CommonStringType[] getOffOnType = CommonStringType.getOffOnType();
        //渠道下拉列表
        List<CommonStringType> firstChannelTypeList = Arrays.asList(getFirstChannelType);
        //在线状态下拉列表
        List<CommonStringType> offOnTypeList = Arrays.asList(getOffOnType);
        
        DateTimeFormatter formater = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        if(pVUVConditionVO.getChooseTimeVO() != null){
            //将日期控件中的时间解析为开始时间和结束时间
            String[] split = pVUVConditionVO.getChooseTimeVO().split("-");
            Date start = DateTime.parse(split[0].trim()+ " 00:00:00", formater).toDate();
            Date end = DateTime.parse(split[1].trim()+ " 23:59:59", formater).toDate();
            startDate=new Date(start.getTime() + 1 * 24 * 60 * 60 * 1000);
            //数据库存储数据规则,当天存储昨日数据,故查询时间加一天
            endDate=new Date(end.getTime() + 1 * 24 * 60 * 60 * 1000);
            
        }else{
            //获取查询的时段(昨日)
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            String dateStr = formatter.format(date).trim();
            startDate = DateTime.parse(dateStr+ " 00:00:00", formater).toDate();
            endDate = DateTime.parse(dateStr+ " 23:59:59", formater).toDate();
            
            //获取昨日选择时段
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
            Date yesterday=calendar.getTime();
            String yesterdayStr = formatter.format(yesterday).trim();
            pVUVConditionVO.setChooseTimeVO(yesterdayStr+" - "+yesterdayStr);
        }
        pVUVConditionVO.setStartDate(startDate);
        pVUVConditionVO.setEndDate(endDate);
        //根据条件查询pv和uv统计量
        PageInfo<PVUV> page = pVUVService.getPVUVList(pVUVConditionVO);
        List<PVUV> pVUVList = page.getList();
        Date create = null;
        Date createDate = null;
        //数据库存储数据规则,当天存储昨日数据,故查询得出的数据创建时间减一天
        for (PVUV pVUV : pVUVList){
            create = pVUV.getCreateDate();
            createDate = new Date(create.getTime() - 1 * 24 * 60 * 60 * 1000);
            pVUV.setCreateDate(createDate);
        }
        //分页
        model.addAttribute("page", page);
        model.addAttribute("pVUVList", pVUVList);
        model.addAttribute("firstChannelTypeList", firstChannelTypeList);
        model.addAttribute("offOnTypeList", offOnTypeList);
        //筛选条件信息
        model.addAttribute("conditionVO", pVUVConditionVO);
        return "application/pvUvList";
    }

}
